﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Security.Cryptography;
using System.IO;
using System.Text;
/// <summary>
/// Summary description for Util
/// </summary>
public class Util
{
    public Util()
    {
    }
    //Phuong thuc ma hoa bang MD5:
    public static string MaHoa(string key, string toEn)
    {
        byte[] keyArray;
        byte[] toEndcry = UTF8Encoding.UTF8.GetBytes(toEn);
        MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
        keyArray = md5.ComputeHash(UTF32Encoding.UTF8.GetBytes(key));
        TripleDESCryptoServiceProvider trip = new TripleDESCryptoServiceProvider();
        trip.Key = keyArray;
        trip.Mode = CipherMode.ECB;
        trip.Padding = PaddingMode.PKCS7;
        ICryptoTransform tranform = trip.CreateEncryptor();
        byte[] resualArray = tranform.TransformFinalBlock(toEndcry, 0, toEndcry.Length);
        return Convert.ToBase64String(resualArray, 0, resualArray.Length);
    }
    //Phuong thuc giai ma bang MD5:
    public static string GiaiMa(string key, string toDe)
    {
        byte[] keyArray;
        byte[] toEndArray = Convert.FromBase64String(toDe);
        MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
        keyArray = md5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
        TripleDESCryptoServiceProvider trip = new TripleDESCryptoServiceProvider();
        trip.Key = keyArray;
        trip.Mode = CipherMode.ECB;
        trip.Padding = PaddingMode.PKCS7;
        ICryptoTransform tranfrom = trip.CreateDecryptor();
        byte[] resualArray = tranfrom.TransformFinalBlock(toEndArray, 0, toEndArray.Length);
        return UTF32Encoding.UTF8.GetString(resualArray);

    }
}